package com.corejava.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

import com.corejava.source.CellBox;
import com.corejava.source.CellVender;
import com.corejava.source.PowerStation;

public class MainSystem {

	private static Properties info;
	private static Properties cellInfo;
	static {
		info = new Properties();
		cellInfo = new Properties();
		try {
			info.load(new FileInputStream("info.properties"));
			cellInfo.load(new FileInputStream("cellinfo.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		mainMenu(sc);
	}

	private static void mainMenu(Scanner sc) {
		while (true) {
			System.out.println("欢迎进入电动汽车充换电站运营管理系统");
			System.out.println("请选择以下功能");
			System.out.println("1: 新增电池箱入库");
			System.out.println("2：查询电池箱");
			System.out.println("3: 更新或删除电池箱");
			System.out.println("0: 退出系统");
			System.out.println("请选择\n\n");
			int choice = sc.nextInt();
			switch (choice) {
			case 1: {
				System.out.println("进入新增电池箱入库功能");
				addCell(sc);
				break;
			}
			case 2: {
				System.out.println("进入查询电池箱功能");
				break;
			}
			case 3: {
				System.out.println("进入更新或删除电池箱功能");
				break;
			}
			case 0: {
				System.out.println("是否退出系统，请输入 Y(y)确认退出，输入其他则返回");
				String s = sc.next();
				if (s.equals("Y") || s.equals("y"))
					System.exit(0);
				else
					break;
			}
			default: {
				System.out.println("输入有误，请重新选择。");
				break;
			}
			}
		}
	}

	public static void addCell(Scanner sc) {
		String[] stations = info.getProperty("stations").split("#");
		System.out.println("请选择要添加电池箱的充电站——列表如下：");
		for (int i = 0; i < stations.length; i++) {
			System.out.println((i + 1) + ":  " + stations[i]);
		}
		System.out.println("请选择,按其他数字返回主菜单");
		int choice = sc.nextInt();
		if (choice < 1 || choice > stations.length) {
			return;
		}
		String choiceStation = stations[choice - 1];

		String[] venders = info.getProperty("venders").split("#");
		System.out.println("请选择所要添加电池箱的厂家");
		for (int i = 0; i < venders.length; i++) {
			System.out.println((i + 1) + ": " + venders[i]);
		}
		System.out.println("请选择,按其他数字返回主菜单");
		choice = sc.nextInt();
		if (choice < 1 || choice > stations.length) {
			return;
		}
		String choiceVender = venders[choice - 1];

		String[] cellModels = info.getProperty(choiceVender).split("#");
		System.out.println("请选择要添加的电池箱的型号");
		for (int i = 0; i < cellModels.length; i++) {
			System.out.println("型号" + (i + 1) + ":  " + cellModels[i]);
		}
		System.out.println("请选择,按其他数字返回主菜单");
		choice = sc.nextInt();
		if (choice < 1 || choice > stations.length) {
			return;
		}
		String choiceCellModel = cellModels[choice - 1];
		CellBox cell = readCellInfo(choiceVender, choiceCellModel);
		System.out.println("是否将电池信息录入" + choiceStation + "中，是则输入1，否则任意输入退回主界面");
		choice = sc.nextInt();
		if (choice != 1) {
			return;
		}
		try {
			saveStationInfo(choiceStation, cell);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static CellBox readCellInfo(String vender, String cellModel) {
		CellVender cellVender = new CellVender(vender, cellModel);
		CellBox cellBox = cellVender.getCellBox();
		System.out.println(cellBox.cellInfoShow());
		return cellBox;
	}

	private static void saveStationInfo(String stationName, CellBox cell)
			throws IOException {
		String stationNum = info.getProperty(stationName);
		Properties stationPro = new Properties();

		stationPro.load(new FileInputStream(stationNum + ".properties"));
		int cellCount = Integer.parseInt(stationPro.getProperty("cellCount")) + 1;
		stationPro.setProperty("cellCount", Integer.toString(cellCount));
		cell.setOrder(cellCount);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		cell.setEnterTime(sdf.format(new Date()));
		stationPro.setProperty(Integer.toString(cellCount), cell.toString());

		// 将该换电站的某种电池类型数量增加1
		stationPro.setProperty(cell.getCellType(), Integer.toString(Integer
				.parseInt(stationPro.getProperty(cell.getCellType()) + 1)));
		FileOutputStream stationFile = new FileOutputStream(stationNum
				+ ".properties");
		stationPro.store(stationFile, "Copyright (c) Boxcode Studio");
		if (stationFile != null) {
			stationFile.close();
		}
	}

	private static void searchCell(Scanner sc) {
		System.out.println("请选择查询的方式：");
		System.out.println("1:  根据换电站查询");
		System.out.println("2:  根据生产厂家和型号查询");
		System.out.println("输入其他则返回主菜单，请选择");
		String choice = sc.next();
		switch (choice) {
		case "1": {
			break;
		}
		case "2": {
			searchForVender(sc);
			break;
		}
		}
	}
	private static void searchForVender(Scanner sc){
		System.out.println("请选择一下厂家");
	}
}
